﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using r4u.data.access.layer;
using r4u.data.access.layer.RECRUIT;

namespace r4u.business.logic.layer.RECRUIT
{
    public class Vacancy
    {
        public bool AddVacancy(string email, VACANCY vacancy)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            return vacancyDao.AddVacancy(email, vacancy);
        }


        public bool UpdateVacancy(VACANCY vacancy)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            return vacancyDao.Update(vacancy);
        }


        public VacancyObject GetVacancy(string vacancy_id)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            VACANCY vacancy = vacancyDao.GetVacancy(vacancy_id);

            VacancyObject v = new VacancyObject();
            v.vacancy_id = vacancy.vacancy_id;
            v.cust_id = vacancy.cust_id;
            v.vacancy_for = vacancy.vacancy_for;
            v.industry = vacancy.industry;
            v.description = vacancy.description;
            v.valid_from = vacancy.valid_from;
            v.valid_to = vacancy.valid_to;
            v.location_of_applicant = vacancy.location_of_applicant;
            v.education_level = vacancy.education_level;
            v.experience_from = vacancy.experience_from;
            v.experience_to = vacancy.experience_to;
            v.age_from = vacancy.age_from;
            v.age_to = vacancy.age_to;
            v.max_applications = vacancy.max_applications;
            v.allow_non_eligible = vacancy.allow_non_eligible;
            v.status = vacancy.status;
            return v;
        }

        public VacancyObject Copy(string email, string vacancy_id)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            VACANCY vacancy = vacancyDao.Copy(email, vacancy_id);

            VacancyObject v = new VacancyObject();
            v.vacancy_id = vacancy.vacancy_id;
            v.cust_id = vacancy.cust_id;
            v.vacancy_for = vacancy.vacancy_for;
            v.industry = vacancy.industry;
            v.description = vacancy.description;
            v.valid_from = vacancy.valid_from;
            v.valid_to = vacancy.valid_to;
            v.location_of_applicant = vacancy.location_of_applicant;
            v.education_level = vacancy.education_level;
            v.experience_from = vacancy.experience_from;
            v.experience_to = vacancy.experience_to;
            v.age_from = vacancy.age_from;
            v.age_to = vacancy.age_to;
            v.max_applications = vacancy.max_applications;
            v.allow_non_eligible = vacancy.allow_non_eligible;
            v.status = vacancy.status;
            return v;
        }

        public VacancyObject[] GetVacancies(string email)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            List<VacancyObject> vacancies = new List<VacancyObject>();

            foreach (VACANCY vacancy in vacancyDao.GetVacancies(email))
            {
                VacancyObject v = new VacancyObject();
                v.vacancy_id = vacancy.vacancy_id;
                v.cust_id = vacancy.cust_id;
                v.vacancy_for = vacancy.vacancy_for;
                v.industry = vacancy.industry;
                v.description = vacancy.description;
                v.valid_from = vacancy.valid_from;
                v.valid_to = vacancy.valid_to;
                v.location_of_applicant = vacancy.location_of_applicant;
                v.education_level = vacancy.education_level;
                v.experience_from = vacancy.experience_from;
                v.experience_to = vacancy.experience_to;
                v.age_from = vacancy.age_from;
                v.age_to = vacancy.age_to;
                v.max_applications = vacancy.max_applications;
                v.allow_non_eligible = vacancy.allow_non_eligible;
                v.status = vacancy.status;
                vacancies.Add(v);
            }

            return vacancies.ToArray();
        }

        public bool DeleteVacancy(string vacancy_id)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            return vacancyDao.DeleteVacancy(vacancy_id);
        }

        public VACANCY GetVacancyWithSkills(string vacancy_id)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            return vacancyDao.GetVacancyWithSkills(vacancy_id);
        }

        public bool InsertApplicant(string vacancy_id, XmlDocument profile)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            string applicant_id = profile.SelectSingleNode("//id").InnerText;
            return vacancyDao.InsertApplicant(vacancy_id, applicant_id, profile);
        }



        public Candidate[] GetApplicants(string vacancy_id)
        {
            VacancyDAO vacancyDao = new VacancyDAO();
            List<Candidate> candidates = new List<Candidate>();

            foreach (VACANCY_APPLICANT applicant in vacancyDao.GetApplicants(vacancy_id))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(applicant.profile_data);
                Candidate c = new Candidate(doc);
                candidates.Add(c);
            }

            return candidates.ToArray();

        }


    }
}
